<?php
/**
 * <p>General purpose Toolbox.</p>
 * <p>Contains functions for general use.</p>
 */
class TOOLBOX {
    private $t;

    /**
     * Descriptor function
     */
    function TOOLBOX() {
        $this->tmrstart();
    }

    function array2table($array,$header,$attr) {
        $retval = "<table ".$attr.">\n";

        $retval = $retval."<tr>\n";
        for($i=0;$i<count($header);$i++) {
            $retval = $retval."<th>".$header[$i]."</th>\n";
        }
        $retval = $retval."</tr>\n";

        for($i=0;$i<count($array);$i++) {
            $retval = $retval."<tr>\n";
            for($j=0;$j<count($array[0]);$j++) {
                $retval = $retval."<td>".$array[$i][$j]."</td>\n";
            }
            $retval = $retval."</tr>\n";
        }
        $retval = $retval."</table>\n";
        return $retval;
    }
    /**
     * Global Unique ID generator
     * @return string
     */
    function guid() {
        return md5(microtime());
    }

    function tmrstart() {
        $this->t = microtime();
    }

    function tmrtotal() {
        echo  "<div align=\"right\"><font size=\"xx-small\" color=\"gray\">Φόρτωση σε: <b>".number_format(((microtime()-$this->t)*1000), 2, '.', '')." msec</b></font></div>";
    }

    function srv() {
        return $_SERVER['SERVER_ADDR']."/book-library-ouc";
    }

    function debug($var) {
        echo "<pre><b>#######################\nBEGIN</b>\n";
        print_r($var);
        echo "\n<b>END\n#######################</b></pre>";
    }
    /**
     * Custom cleaner for user input cleaning
     */
    function cleaner($value) {
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }
        $value = trim($value);
        return $value;
    }

    function cleantext($value) {
        return nl2br(htmlentities($this->cleaner(preg_replace('/[^α-ωΑ-Ωa-zA-Z0-9 .]/', '', $value))));
    }

    /**
     * Custom cleaner for user input cleaning
     */
    function sqlcleaner($value) {
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }
        if (!is_numeric($value)) {
            $value = mysql_real_escape_string($value);
        }
        $value = trim($value);
        return $value;
    }

    /**
     * If user is administrator function
     * returns librarian and vise versa.
     * @param string $adminlib
     * @return string
     */
    function canregister($adminlib) {
        if($adminlib == ADMIN) {
            return LIB;
        }else if($adminlib == LIB) {
            return USER;
        }
    }
}

?>
